package com.td.module.produce.controller.admin.inputhusbandry;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.produce.controller.admin.inputhusbandry.vo.*;
import com.td.module.produce.convert.inputhusbandry.InputHusbandryConvert;
import com.td.module.produce.dal.dataobject.inputhusbandry.InputHusbandryDO;
import com.td.module.produce.service.inputhusbandry.InputHusbandryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 投入品与农事中间")
@RestController
@RequestMapping("/produce/input-husbandry")
@Validated
public class InputHusbandryController {

    @Resource
    private InputHusbandryService inputHusbandryService;

    @PostMapping("/create")
    @ApiOperation("创建投入品与农事中间")
    @PreAuthorize("@ss.hasPermission('produce:input-husbandry:create')")
    public CommonResult<Long> createInputHusbandry(@Valid @RequestBody InputHusbandryCreateReqVO createReqVO) {
        return success(inputHusbandryService.createInputHusbandry(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新投入品与农事中间")
    @PreAuthorize("@ss.hasPermission('produce:input-husbandry:update')")
    public CommonResult<Boolean> updateInputHusbandry(@Valid @RequestBody InputHusbandryUpdateReqVO updateReqVO) {
        inputHusbandryService.updateInputHusbandry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除投入品与农事中间")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('produce:input-husbandry:delete')")
    public CommonResult<Boolean> deleteInputHusbandry(@RequestParam("id") Long id) {
        inputHusbandryService.deleteInputHusbandry(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得投入品与农事中间")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('produce:input-husbandry:query')")
    public CommonResult<InputHusbandryRespVO> getInputHusbandry(@RequestParam("id") Long id) {
        InputHusbandryDO inputHusbandry = inputHusbandryService.getInputHusbandry(id);
        return success(InputHusbandryConvert.INSTANCE.convert(inputHusbandry));
    }

    @GetMapping("/list")
    @ApiOperation("获得投入品与农事中间列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('produce:input-husbandry:query')")
    public CommonResult<List<InputHusbandryRespVO>> getInputHusbandryList(@RequestParam("ids") Collection<Long> ids) {
        List<InputHusbandryDO> list = inputHusbandryService.getInputHusbandryList(ids);
        return success(InputHusbandryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得投入品与农事中间分页")
    @PreAuthorize("@ss.hasPermission('produce:input-husbandry:query')")
    public CommonResult<PageResult<InputHusbandryRespVO>> getInputHusbandryPage(@Valid InputHusbandryPageReqVO pageVO) {
        PageResult<InputHusbandryDO> pageResult = inputHusbandryService.getInputHusbandryPage(pageVO);
        return success(InputHusbandryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出投入品与农事中间 Excel")
    @PreAuthorize("@ss.hasPermission('produce:input-husbandry:export')")
    @OperateLog(type = EXPORT)
    public void exportInputHusbandryExcel(@Valid InputHusbandryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InputHusbandryDO> list = inputHusbandryService.getInputHusbandryList(exportReqVO);
        // 导出 Excel
        List<InputHusbandryExcelVO> datas = InputHusbandryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "投入品与农事中间.xls", "数据", InputHusbandryExcelVO.class, datas);
    }

}
